<template>
  <!-- 带搜索的树形控件 -->
  <div ref="treeBox">
    <div v-if="(treeData && treeData.length > 0) || spinning || !rootNode">
      <!-- 页眉插槽：输出选中节点信息和节点操作 -->
      <slot
        name="header"
        :selectedKeys="selectedKeys"
        :selectOptions="selectOptions"
        :handleOperate="handleOperate"
        :handleStopNode="handleStopNode"
      />
      <!-- 树搜索 -->
      <a-input-search
        v-if="showSearch"
        class="mb12"
        placeholder="请输入"
        style="width: 100%"
        :loading="loading"
        @search="onSearch"
      />
      <!-- 树节点 -->
      <a-spin
        :spinning="spinning"
        class="treeBox"
        :style="[{ height: defaultHeight || treeHeight, minHeight: minHeight, overflow: 'auto' }]"
        :class="{ mb12: showButton }"
      >
        <a-tree
          v-bind="$attrs"
          v-on="$listeners"
          :auto-expand-parent="autoExpandParent"
          :replace-fields="replaceFields"
          :expanded-keys.sync="expandedKeys"
          :selected-keys.sync="selectedKeys"
          v-model="checkedKeys"
          :load-data="lazyLoadFn ? handleLoadData : null"
          :tree-data="treeData"
          @expand="onExpand"
          @select="onSelect"
        >
          <template
            slot="name"
            slot-scope="data"
          >
            <slot
              name="nameBefore"
              :data="data"
            />

            <span v-if="data.name && data.name.indexOf(searchValue) > -1">
              <span>{{ data.name.substr(0, data.name.indexOf(searchValue)) }}</span>
              <span style="color: #ff4d4f">{{ searchValue }}</span>
              <span>{{ data.name.substr(data.name.indexOf(searchValue) + searchValue.length) }}</span>
              <span v-if="addCount">({{ data.count }})</span>
            </span>
            <span v-else>{{ data.name }}</span>
            <span
              v-if="data.status == 1"
              style="color: #ff4d4f"
            >{{ '（停用）' }}</span>
            <slot
              name="nameAfter"
              :data="data"
              @click="$console(data)"
            />
          </template>
        </a-tree>
      </a-spin>
      <!-- 树操作栏 -->
      <div
        v-if="showButton"
        ref="btnBox"
        align="center"
      >
        <a-button
          v-if="isReference"
          size="small"
          class="btn_right"
          type="primary"
          @click="reference()"
        >
          引用
        </a-button>
        <a-button
          size="small"
          :disabled="
            (selectedKeys && selectedKeys.length < 1) || (selectOptions && selectOptions.isQuote == 0 && quote)
          "
          class="btn_right"
          type="primary"
          @click="handleOperate(0)"
        >
          添加
        </a-button>

        <a-button
          size="small"
          :disabled="
            (selectedKeys && selectedKeys.length < 1) ||
              (selectOptions && selectOptions.isQuote == 0 && quote) ||
              selectOptions.id == treeData[0].id
          "
          class="btn_right"
          type="primary"
          @click="handleOperate(1)"
        >
          编辑
        </a-button>
        <!-- 添加跟节点不可删除 -->
        <a-button
          v-if="isDelete"
          size="small"
          type="danger"
          :disabled="(selectedKeys && selectedKeys.length < 1) || selectOptions.id == treeData[0].id"
          @click="handleStopNode('del')"
        >
          删除
        </a-button>
      </div>

      <!-- 页脚插槽：输出选中节点信息和节点操作 -->
      <slot
        name="footer"
        :selectedKeys="selectedKeys"
        :selectOptions="selectOptions"
        :handleOperate="handleOperate"
        :handleStopNode="handleStopNode"
      />
    </div>
    <div v-else>
      <div :style="[{ height: defaultHeight || treeHeight, minHeight: minHeight, overflow: 'auto' }]">
        <empty :description="emptyDescription" />
      </div>
      <div
        ref="btnBox"
        align="center"
      >
        <a-button
          v-if="isReference"
          size="small"
          class="btn_right"
          type="primary"
          @click="reference()"
        >
          引用
        </a-button>
      </div>
    </div>

    <!-- 节点表单弹窗 -->
    <dialog-node
      ref="dialogNode"
      :visible.sync="visible"
      :form="form"
      :form-list="defaultFormList"
      :header-title="headerTitle"
      @confirm="submitForm"
    />
  </div>
</template>

<script>
import { getJobs } from '@/api/subassembly/TreeSelectOrganization'
import dialogNode from './dialogNode' // 节点表单弹窗
import { Empty } from 'ant-design-vue' // 无数据提示
import cloneDeep from 'lodash.clonedeep' // 深拷贝
import { mapGetters } from 'vuex'
export default {
  name: 'TreeSearch',
  components: {
    dialogNode,
    Empty
  },
  data() {
    return {
      loading: false, // 搜索加载控件
      editModel: 0, // 操作类型：0 添加 1 编辑
      visible: false, // 弹窗控件
      form: {}, // 弹窗参数
      selectOptions: {}, // 选中节点对象
      selectedKeys: [], // 选中节点
      treeHeight: 0, // 节点树高度
      treeData: [], // 树节点列表
      spinning: false, // 树加载框控件
      expandedKeys: [], // 展开节点
      autoExpandParent: true, // 自动展开父级节点
      allList: [],
      // dataList: [], // 树节点列表（一级）
      searchValue: '', // 关键词
      level: '1', // 层级,
      showQualityButtonDisabled: false, // 质量按钮禁用,
      userInfo: JSON.parse(sessionStorage.getItem('ycloud-user_info'))
    }
  },
  props: {
    // 请求参数id
    id: {
      type: [String, Number],
      default() {
        return ''
      }
    },
    // 请求参数
    query: {
      type: [Object],
      default() {
        return null
      }
    },
    //是否显示引用按钮
    isReference: {
      type: Boolean,
      default: () => false
    },
    // 空白描述
    emptyDescription: {
      type: String,
      default() {
        return '暂无分部分项信息'
      }
    },
    minHeight: {
      type: String,
      default: '200px'
    },
    // api请求
    api: {
      type: [Object, Function]
    },
    // api请求方法
    loadList: {
      type: String,
      default: function () {
        return 'getList'
      }
    },
    // 节点键名
    itemName: {
      type: String,
      default: function () {
        return ''
      }
    },
    // 树列表参数配置
    replaceFields: {
      type: Object,
      default() {
        return { children: 'children', title: 'name', key: 'id' }
      }
    },
    // 是否可以搜索
    showSearch: {
      type: Boolean,
      default: () => true
    },
    // 是否显示操作按钮
    showButton: {
      type: Boolean,
      default: () => false
    },
    showQualityButton: {
      type: Boolean,
      default: () => false
    },
    // 展示分包商责任区域树的按钮
    showSubcontrctorBtn: {
      type: Boolean,
      default: () => false
    },
    // 是否显示所有，默认显示启用状态的树
    isAll: {
      type: Boolean,
      default: () => false
    },
    // 懒加载函数，设置为null时，表示不需要懒加载
    lazyLoadFn: {
      type: [Function, Object],
      default: null
    },
    // 树的固定高度
    defaultHeight: {
      type: [String, Number],
      default: () => 0
    },
    // 是否自动选中首节点
    autoSelect: {
      type: Boolean,
      default: () => true
    },
    // 组织节点是否可选
    orgSelectable: {
      type: Boolean,
      default: () => true
    },
    // 添加按钮禁用状态
    addDisable: {
      type: Boolean,
      default: () => false
    },
    // 节点列表
    datas: {
      type: [Array],
      default: () => []
    },
    // 是否禁用停用的选项
    isDisabled: {
      type: Boolean,
      default: () => false
    },
    // 是否禁用父级节点
    parentDisabled: {
      type: Boolean,
      default: () => false
    },
    // 是否可以选择父级节点
    parentSelectable: {
      type: Boolean,
      default: () => false
    },
    // 回调请求列表字段名
    dataName: {
      type: String,
      default: () => ''
    },
    //没有根节点
    rootNode: {
      type: Boolean,
      default: () => true
    },
    isDelete: {
      type: Boolean,
      default: () => false
    },
    // 提交请求参数
    requestArgs: {
      type: Object,
      default: () => {}
    },
    // 限制树级数
    levelLimit: {
      type: [Number, String, Object],
      default: () => null
    },
    //删除项目所在树节点
    projectDeptOrgId: {
      type: String,
      default: () => ''
    },
    // 搜索结果是否保留子级
    showChildWithSearch: {
      type: Boolean,
      default: false
    },
    // 该属性是一个优化项，避免搜索值为空时，递归耗时过长的问题,为 true 时启用
    expandByAuto: {
      type: Boolean,
      default: false
    },
    // 初始化时展开所有节点
    initExpandAll: {
      type: Boolean,
      default: true
    },
    // 过滤函数，对每一个数据项进行过滤，接收参数为遍历的数据项（只针对树的第二级过滤）
    filterFn: {
      type: Function,
      default: null
    },
    defaultSelectedKeys: {
      type: [Array],
      default: () => []
    },
    //弹框表单
    formList: {
      type: Array,
      default() {
        return []
      }
    },
    //添加计数
    addCount: {
      type: Boolean,
      default: true
    },
    outCheckedKeys: {
      type: [Array],
      default: () => []
    },
    // 禁用节点是否显示
    showNode: {
      type: Boolean,
      default: false
    },
    // 引用是否可编辑
    quote: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    ...mapGetters(['GET_REDRESS_PERCENTAGE']),
    // 默认弹窗表单配置项
    defaultFormList() {
      let defaultList = [
        {
          // hidden: this.form?.parent ? false : true,
          keyName: 'parent',
          type: 'BaseInput',
          disabled: true,
          span: 24,
          label: '父节点'
        },
        {
          keyName: 'name',
          type: 'BaseInput',
          span: 24,
          label: '节点名'
        }
      ]
      return this.formList.length > 0 ? defaultList.concat(this.formList) : defaultList
    },
    // 弹窗动态标题
    headerTitle() {
      return ['新增节点', '编辑节点'][this.editModel]
    },
    // 节点列表
    dataList: {
      get: function () {
        return this.datas
      },
      set: function (val) {
        this.$emit('update:datas', val)
      }
    },
    // 输出选中节点
    checkedKeys: {
      get: function () {
        return this.outCheckedKeys
      },
      set: function (val) {
        this.$emit('update:outCheckedKeys', val)
      }
    }
  },
  mounted() {
    if (!this.defaultHeight) {
      // 树节点盒子自适应高度
      this.autoHeight()
      // 监听窗口,设置树节点盒子自适应高度
      window.addEventListener('resize', this.autoHeight)
    }
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.autoHeight)
  },
  watch: {
    defaultSelectedKeys(val) {
      if (val && val.length) {
        this.selectedKeys = val
      }
    },
    // 输出选中节点
    selectedKeys(val) {
      this.$emit('update:selectedKeys', val)
    },
    // 输出选中节点对象
    selectOptions(val) {
      this.$emit('update:selectOptions', val)
    },
    id(val) {
      this.selectOptions = {}
    }
  },
  methods: {
    //点击引用事件
    reference() {
      this.$emit('reference')
    },
    // 懒加载数据
    handleLoadData(treeNode) {
      const { id } = treeNode.dataRef
      return new Promise(async (resolve, reject) => {
        if (treeNode.dataRef.children) {
          resolve()
          return
        }
        try {
          const res = await this.lazyLoadFn(id)
          const list = res.data || []
          const { title, key } = this.replaceFields
          treeNode.dataRef.children = list.map((item) => {
            return {
              ...item,
              [title]: item.title,
              [key]: item.key,
              children: [],
              isLeaf: true
            }
          })
          this.treeData = [...this.treeData]
          resolve()
        } catch (e) {
          reject()
        }
      })
    },
    // 生成全部列表
    generateList(data, levels) {
      for (let i = 0; i < data.length; i++) {
        const node = data[i] // 节点
        let level = levels || this.level // 层级
        node.level = level // 添加树层级
        node.selectable = this.orgSelectable // 设置节点是否可选状态
        //禁用传入的项目部
        if (node.id == this.projectDeptOrgId) {
          console.log(node)
          node.disabled = true
        }

        this.isDisabled && (node.disabled = !node.enabled)

        // 禁用有停用状态的节点
        const id = node[this.replaceFields.key]
        node.name = node[this.replaceFields.title]
        node.hasChildren = node[this.replaceFields.children]?.length > 0 // 添加是否有子级标识
        this.parentDisabled && (node.disabled = node.hasChildren) // 禁用父级
        this.parentSelectable && (node.selectable = !node.hasChildren) // 禁用父级
        // 若不需要显示全部，则根据停用状态隐藏节点
        if (node.status == 1 && this.showNode) {
          data.splice(i, 1)
          // console.log(data)
          i--
          continue
        }
        // 设置节点name插槽
        this.$set(node, 'scopedSlots', { title: 'name' })
        let addItem = {}
        addItem[this.replaceFields.key] = id
        addItem[this.replaceFields.title] = node[this.replaceFields.title]
        addItem.name = node[this.replaceFields.title]
        addItem.enabled = node.enabled
        addItem.hasChildren = node.children?.length > 0
        addItem.disabled = node.disabled
        addItem.orgType = node.orgType ? node.orgType : null
        // 单级节点深拷贝数组
        this.dataList.push(addItem)
        // 单级节点浅拷贝数组
        this.allList.push(node)
        // 若限制层级则去除children
        if (level == this.levelLimit) {
          node.list = node.children
          delete node.children
          continue
        }
        // 若有children则继续遍历
        if (node.children) {
          level++
          this.generateList(node.children, level)
        }
      }
    },
    // 获取父节点
    getParentKey(key, tree) {
      let parentKey
      for (let i = 0; i < tree.length; i++) {
        const node = tree[i]
        this.$set(node, 'scopedSlots', { title: 'name' })
        if (node.children) {
          if (node.children.some((item) => item.id === key)) {
            parentKey = node.id
          } else if (this.getParentKey(key, node.children)) {
            parentKey = this.getParentKey(key, node.children)
          }
        }
      }
      return parentKey
    },
    // 挂载后设置树的高度
    autoHeight() {
      this.$nextTick(() => {
        let top = this.$refs.treeBox ? this.$refs.treeBox.getBoundingClientRect().top : 0 // 树离窗口上边距
        let bottom = this.$refs.btnBox ? this.$refs.btnBox.getBoundingClientRect().height : 0 // 操作栏高度
        this.treeHeight = `calc(${GET_REDRESS_PERCENTAGE}vh - ${top + bottom + 130}px`
      })
    },
    setExpandKeys(list = []) {
      return [...list].filter((item) => item[this.replaceFields.children]?.length > 0).map((item) => item[this.replaceFields.key])
    },
    // 获取列表
    getList(isRefesh = false) {
      // 加载
      this.spinning = true
      // 重置选中节点
      this.selectedKeys = []
      // 若路由有参数则加上
      let args = {}
      if (this.$route.params.id) {
        args.id = this.$route.params.id
      }
      if (this.id) {
        args.id = this.id
      }
      // 若有参数则加上
      let query = this.query
      query && Object.keys(query).length > 0 && Object.assign(args, query)

      console.log(args)
      // 返回axiaos（promise对象）
      return this.api[this.loadList](args)
        .then((res) => {
          // 停止加载
          this.spinning = false
          if (res.code == 200 || res.data) {
            this.allList = []
            // 格式化树形列表
            this.treeData = this.dataName ? res.data[this.dataName] || [] : res.data

            // 只针对树的第二级过滤
            if (this.filterFn) {
              this.treeData = this.treeData.map((item) => {
                if (item.children) {
                  item.children = item.children.filter(this.filterFn)
                }
                return item
              })
            }
            this.$emit('getData', res.data)
            // 生成一级全部节点
            this.generateList(this.treeData)
            // 设置展开节点
            if (this.initExpandAll) {
              this.expandedKeys = this.setExpandKeys(this.allList)
            }
            // 初始化选中对象
            if (!this.selectOptions[this.replaceFields.key]) {
              this.selectOptions = res.data[0]
              //   parent: res.data[0].parentName,
              //   parentId: res.data[0].parentId,
              //   enabled: res.data[0].enabled
              // }
              this.selectOptions[this.replaceFields.title] = res.data[0][this.replaceFields.title]
              this.selectOptions[this.replaceFields.key] = res.data[0][this.replaceFields.key]
              this.selectedKeys = [this.selectOptions[this.replaceFields.key]]
            } else {
              this.$nextTick(() => {
                this.selectOptions = this.allList.find((item) => item[this.replaceFields.key] === this.selectOptions[this.replaceFields.key]) || {}
                this.selectedKeys = [this.selectOptions[this.replaceFields.key]]
              })
            }
            // console.log( this.selectedKeys, ' this.selectedKeys')
            // if (
            //   this.selectOptions.parentId != null &&
            //   !this.expandedKeys.some((item) => this.selectOptions.parentId == item[this.replaceFields.key])
            // ) {
            //   this.expandedKeys.push(this.selectOptions.parentId)
            // }
            // 默认选中
            if (this.autoSelect) {
              this.selectedKeys = this.selectOptions[this.replaceFields.key]
                ? [this.selectOptions[this.replaceFields.key]]
                : res.data[0].enabled
                ? [res.data[0][this.replaceFields.key]]
                : res.data[0].enabled != undefined
                ? []
                : [res.data[0][this.replaceFields.key]]
            }
            if(isRefesh) {
              this.$nextTick(() => {
                this.$emit('loader')
              })
            }
          }
        })
        .catch((res) => {
          // 停止加载
          this.spinning = false
        })
    },
    // 选中节点
    onSelect(e, b) {
      let data = {}
      if (e.length > 0) {
        // 设置选中节点对象
        data = b.selectedNodes[0].data.props
        this.selectOptions = Object.assign({}, data, {
          enabled: data.enabled,
          parent: data.parentName,
          // name: data.name,
          // id: e[0],
          parentId: data.parentId
        })
        this.selectOptions[this.replaceFields.title] = data[this.replaceFields.title]
        this.selectOptions[this.replaceFields.key] = e[0]
        // 搜索列表
        this.$nextTick(() => {
          this.$emit('loader')
        })
      }
      this.$emit('onSelect', data.data)
    },
    // 伸展树节点
    onExpand(expandedKeys) {
      this.expandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    // 树节点搜索
    onSearch(e) {
      const value = e?.target?.value || e
      this.loading = true
      setTimeout(() => {
        const linkList = []
        //   若关键词存在
        if (value) {
          // 检索关键词，非关联节点设置不显示
          for (let index in this.allList) {
            let item = this.allList[index]
            if (item[this.replaceFields.title]?.indexOf(value) === -1) {
              item.style = { display: 'none' }
            } else {
              item.style && delete item.style
            }
          }
          // 将关联节点的父级全部显示
          for (let index in this.allList) {
            let item = this.allList[index]
            if (item[this.replaceFields.title]?.indexOf(value) > -1) {
              linkList.push(item)
              this.setParent(item)
              this.showChildWithSearch && this.setChild(item)
            }
          }
          // 节点的子节点集合children为空时，取消叶节点
          if (!this.showChildWithSearch) {
            for (let index in this.allList) {
              let item = this.allList[index]
              // 当子节点不存在或者全部隐藏时，取消节点
              if (!item[this.replaceFields.children] || item[this.replaceFields.children]?.every((i) => (i.style ? true : false))) {
                item.isLeaf = true
              } else {
                item.isLeaf = false
              }
            }
          }
        } else {
          // 搜索值为空的事件
          this.$emit('searchEmpty')
          // 取消隐藏节点，且节点的子节点集合children为空时，取消叶节点
          for (let index in this.allList) {
            let item = this.allList[index]
            item.style && delete item.style
            if (item[this.replaceFields.children]?.length > 0) {
              item.isLeaf = false
            } else {
              item.isLeaf = true
            }
          }
        }
        // 不初始化展开的话，直接将展开数组置空，然后返回即可
        if (!this.initExpandAll && !value) {
          Object.assign(this, {
            expandedKeys: [],
            searchValue: value,
            autoExpandParent: true
          })
          this.loading = false
          return
        }
        let expandedKeys = []
        // 该属性是一个优化项，避免搜索值为空时，递归耗时过长的问题（防止对以前的代码造成影响，因此用if-else区分）
        if (this.expandByAuto) {
          if (!value) {
            expandedKeys = this.setExpandKeys(this.allList)
          } else {
            linkList.forEach((item) => {
              const parentKey = item.pid || item.parentId
              if (!expandedKeys.includes(parentKey)) {
                expandedKeys.push(parentKey)
              }
              expandedKeys.push(item[this.replaceFields.key])
            })
          }
        } else {
          expandedKeys = this.dataList
            .map((item) => {
              if (item[this.replaceFields.title]?.indexOf(value) > -1) {
                return this.getParentKey(item[this.replaceFields.key], this.treeData)
              }
              return null
            })
            .filter((item, i, self) => item && self?.indexOf(item) === i)
        }
        Object.assign(this, {
          expandedKeys,
          searchValue: value,
          autoExpandParent: true
        })
        this.loading = false
      })
    },
    /**
     * 遍历父节点设置其为显示
     */
    setParent(item) {
      for (let index in this.allList) {
        let i = this.allList[index]
        if (i[this.replaceFields.key] === item.parentId || i[this.replaceFields.key] === item.pid) {
          i.style && delete this.allList[index].style
          if (i.parentId || i.pid) this.setParent(this.allList[index])
          return
        }
      }
    },
    /**
     * 设置目标节点的子节点显示
     */
    setChild(item) {
      for (let index in this.allList) {
        let i = this.allList[index]
        if (i.pid === item[this.replaceFields.key] || i.parentId === item[this.replaceFields.key]) {
          i.style && delete this.allList[index].style
        }
      }
    },
    // 节点：新增/编辑
    handleOperate(a) {
      this.editModel = a
      let form = {}
      if (a == 0) {
        if (Object.keys(this.selectOptions).length > 0) {
          form[this.replaceFields.title] = ''
          form[this.replaceFields.key] = this.selectOptions[this.replaceFields.key]
          form.parentId = this.selectOptions.parentId
          form.parent = this.selectOptions[this.replaceFields.title]
          form.status = 0
          this.form = form
          this.visible = true // 打开弹窗
        }
      } else {
        this.$refs.dialogNode.loading = true
        this.api.getDetails({ id: this.selectOptions.id }).then((res) => {
          this.$refs.dialogNode.loading = false
          if (res.code == 200 || res.data.code == 200 || res.code == 0) {
            form = cloneDeep(res.data)
            form.parent = form.parentName
            form.name = form.groupName
            this.form = form
          }
        })
        // form = cloneDeep(this.selectOptions)
      }
      // let form =
      //   a == 0
      //     ? {
      //         id: this.selectOptions.id,
      //         name: '',
      //         parent: this.selectOptions.name,
      //         parentId: this.selectOptions.parentId,
      //       }
      //     : this.selectOptions
      // this.form = form
      this.visible = true // 打开弹窗
    },
    // 停用节点
    handleStopNode(type) {
      let that = this
      let e = that.selectOptions
      Object.assign(e, this.requestArgs)
      if (type == 'del') {
        that.$confirm({
          title: '温馨提示',
          content: `确认删除该分类，及其所有子分类/规范清单吗？`,
          okText: '确认',
          cancelText: '关闭',
          onOk() {
            return that.api.delete(e).then((res) => {
              if (res.code == 200 || res.data.code == 200 || res.code == 0) {
                that.$message.success('删除成功！')
                that.selectOptions =  {}
                that.getList(true)
              }
            })
          }
        })
      } else {
        console.log(e)
        let enabled = e.enabled ? '停用' : '启用'
        that.$confirm({
          title: '提示',
          content: `确认${enabled}？`,
          okText: '提交',
          cancelText: '关闭',
          onOk() {
            return that.api.switchStatus(e).then((res) => {
              if (res.code == 200 || res.data.code == 200) {
                that.$message.success(enabled + '成功！')
                that.getList()
              }
            })
          }
        })
      }
    },
    // 质量树删除
    handleDel() {
      let that = this
      that.$confirm({
        title: '温馨提示',
        content: `确认删除？`,
        okText: '确认',
        cancelText: '关闭',
        onOk() {
          return that.api
            .delete({ id: that.selectOptions.id })
            .then((res) => {
              if (res.code == 200 || res.data.code == 200) {
                that.$message.success('删除成功！')
                that.getList()
                that.$emit('loader')
              }
            })
            .catch((error) => {
              console.log(error)
            })
        }
      })
    },
    // 提交表单
    submitForm(e) {
      let that = this
      let name = ['create', 'update'][that.editModel] // 根据操作切换url地址
      let form = this.$clone(Object.assign(this.form, this.requestArgs))
      form.parent && delete form.parent
      form.parentId && that.editModel != 0 && delete form.parentId // 若传入表单参数有parentId则不删除
      // 质量添加
      if (this.showQualityButton) {
        form.sources = '4'
      } else {
        form.sources = '0'
      }
      // 提交表单
      this.api[name](form)
        .then((res) => {
          if (res.code === 200 || res.data.code === 200 || res.code == 0) {
            that.$refs.dialogNode.loading = false
            // that.editModel == 0 && (that.selectOptions = res.data)
            that.$message.success(['添加', '编辑'][that.editModel] + '成功！')
            that.visible = false // 关闭弹窗
            that.getList()
          }
        })
        .catch((res) => {
          that.$refs.dialogNode.loading = false
        })
    }
  }
}
</script>
<style lang="less" scoped>
.treeBox {
  // height: calc(65vh - 96px);
  overflow: scroll;
  scrollbar-width: thin;
  scrollbar-color: #ccc transparent;
  scrollbar-track-color: #fff;
  -ms-scrollbar-track-color: transparent;
  &::-webkit-scrollbar {
    background: #fff;
    width: 8px;
    height: 8px;
    // display: none;
  }
  &::-webkit-scrollbar-thumb {
    /*滚动条里面小方块*/
    width: 8px;
    border-radius: 8px;
    border: 1px #fff solid;
    background: #ccc;
  }
}
</style>
